﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Lead2012.Business
{
    public class FieldCustomRepository
    {
        // Create an instance of entity
        Lead2012Entities entities = new Lead2012Entities();

        public List<FieldCustom> GetAllByGroup(int groupId)
        {
            return (from c in entities.FieldCustoms
                    where c.GroupId == groupId
                    orderby c.OrderId ascending
                    select c).ToList();
        }
        
        public FieldCustom GetSingle(int id)
        {
            return (from _object in entities.FieldCustoms where _object.FieldId == id select _object).FirstOrDefault();
        }

        public void Create(FieldCustom _object)
        {
            entities.AddToFieldCustoms(_object);
            entities.SaveChanges();
        }

        public void Update(FieldCustom _object)
        {
            entities.SaveChanges();
        }
        public void Update(FieldCustom _object, int groupId)
        {
            _object.FieldGroupReference.EntityKey = new EntityKey("Lead2012Entities.FieldGroups", "GroupId", groupId);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.FieldCustoms where _list.FieldId == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }
    }
}